package com.example.sprinbootdemo.controller;

import com.example.sprinbootdemo.entity.ApiResponse;
import com.example.sprinbootdemo.entity.Renter;
import com.example.sprinbootdemo.service.RenterService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/renters")
public class RenterController {

    private static final Logger logger = LoggerFactory.getLogger(RenterController.class);

    @Autowired
    private RenterService renterService;

    // 获取所有的租赁者记录
    @GetMapping("/getList")
    public ResponseEntity<ApiResponse> getList() {
        List<Renter> list;
        try {
            list = renterService.getAllRenters();
            ApiResponse response = new ApiResponse(1, "成功", list);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            logger.error("获取租赁者记录列表失败", e);
            ApiResponse response = new ApiResponse(0, "获取租赁者记录列表失败: " + e.getMessage(), null);
            return ResponseEntity.status(500).body(response);
        }
    }
    // 添加新的租赁者记录
    @PostMapping("/addDate")
    public ResponseEntity<ApiResponse> addRenter(@RequestBody Renter renter) {
        try {
            logger.info("添加租赁者记录: {}", renter);
            renterService.addRenter(renter);
            ApiResponse response = new ApiResponse(1, "租赁者记录添加成功", null);
            return ResponseEntity.status(201).body(response); // 返回 201 表示创建成功
        } catch (Exception e) {
            logger.error("添加租赁者记录失败", e);
            ApiResponse response = new ApiResponse(0, "添加租赁者记录失败: " + e.getMessage(), null);
            return ResponseEntity.status(500).body(response);
        }
    }

    // 删除指定 ID 的租赁者记录
    @DeleteMapping("/delete/{renterId}")
    public ResponseEntity<ApiResponse> deleteRenterByRenterID(@PathVariable int renterId) {
        try {
            logger.info("删除租赁者记录, RenterID: {}", renterId);
            renterService.deleteRenterById(renterId);
            ApiResponse response = new ApiResponse(1, "租赁者记录删除成功", null);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            logger.error("删除租赁者记录失败, RenterID: {}", renterId, e);
            ApiResponse response = new ApiResponse(0, "删除租赁者记录失败: " + e.getMessage(), null);
            return ResponseEntity.status(500).body(response);
        }
    }

    // 更新指定 ID 的租赁者记录
    @PutMapping("/update")
    public ResponseEntity<ApiResponse> updateRenterByRenterID(@RequestBody Renter renter) {
        try {
            logger.info("更新租赁者记录: {}", renter);
            renterService.updateRenterById(renter);
            ApiResponse response = new ApiResponse(1, "租赁者记录更新成功", null);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            logger.error("更新租赁者记录失败", e);
            ApiResponse response = new ApiResponse(0, "更新租赁者记录失败: " + e.getMessage(), null);
            return ResponseEntity.status(500).body(response);
        }
    }

    // 根据姓名查询租赁者记录
    @GetMapping("/searchByName/{name}")
    public ResponseEntity<ApiResponse> getRentersByName(@PathVariable String name) {
        List<Renter> renters;
        try {
            renters = renterService.getRentersByName(name);
            ApiResponse response = new ApiResponse(1, "成功", renters);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            logger.error("根据姓名查询租赁者记录失败, Name: {}", name, e);
            ApiResponse response = new ApiResponse(0, "根据姓名查询租赁者记录失败: " + e.getMessage(), null);
            return ResponseEntity.status(500).body(response);
        }
    }
}